package com.easysoft.framework.action;

import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.product.model.SysProductCategory;
import com.easysoft.framework.product.model.SysProductCategoryQuery;
import com.easysoft.framework.product.model.SysProductCategoryView;
import com.easysoft.framework.product.service.SysProductCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/9/12.
 * <p>
 * 产品分类表
 */
@RestController
@RequestMapping(value = SystemProductCategoryAction.BASE_VIEW)
public class SystemProductCategoryAction {

    public static final String BASE_VIEW = "/product";
    private final static Logger logger = LoggerFactory.getLogger(SystemProductCategoryAction.class);

    @Autowired
    @Qualifier("service.SysProductCategoryService")
    private SysProductCategoryService productCategoryService;

    @RequestMapping(value = "/listCategory", method = RequestMethod.GET)
    public ModelAndView listCategory(Page page, SysProductCategoryQuery query, HttpSession session) {
        logger.info("Call SystemProductCategoryAction.listCategory({},{})", page.toString(), query.toString());
        List<SysProductCategoryView> listCategory = productCategoryService.selectSysProductCategoryByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listProductCategory");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listCategory", listCategory);
        return mv;
    }

    @RequestMapping(value = "/editCategory", method = RequestMethod.GET)
    public ModelAndView editCategory(String action, @RequestParam(required = false) String id) {
        logger.info("Call SystemProductCategoryAction.editCategory({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editProductCategory");
        List<SysProductCategoryView> listProductCategory = productCategoryService.selectSysProductCategory(true);
        mv.addObject("listProductCategory", listProductCategory);
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysProductCategory entity = productCategoryService.selectSysProductCategoryById(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveCategory", method = RequestMethod.POST)
    public int saveCategory(String action, SysProductCategory category, HttpSession session) {
        logger.info("Call SystemProductCategoryAction.saveCategory({},{})", action, category.toString());
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(category.getId())) {
                return 0;
            }
            return addSysProductCategory(category, session);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(category.getId())) {
                return 0;
            }
            return updateSysProductCategory(category, session);
        }
        return 0;
    }

    @SystemControllerLog(description = "新增商品分类信息")
    private int addSysProductCategory(SysProductCategory category, HttpSession session) {
        logger.info("Call SystemProductCategoryAction.addSysProduct({})", category.toString());
        category.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        if (category.getParentNodes().equals("0")) {
            category.setNodePath("0");
        } else {
            category.setNodePath("0," + category.getParentNodes());
        }
        return productCategoryService.addSysProductCategory(category);
    }

    @SystemControllerLog(description = "修改商品分类信息")
    private int updateSysProductCategory(SysProductCategory category, HttpSession session) {
        logger.info("Call SystemProductCategoryAction.updateSysProduct({})", category.toString());
        category.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return productCategoryService.updateSysProductCategory(category);
    }

    @RequestMapping(value = "/deleteCategory", method = RequestMethod.POST)
    @SystemControllerLog(description = "删除商品分类信息")
    public int deleteSysProductCategory(String id) {
        logger.info("Call SystemProductCategoryAction.deleteSysTrademark({})", id);
        return productCategoryService.deleteSysProductCategory(id);
    }

    @RequestMapping(value = "/updateCategoryAble", method = RequestMethod.POST)
    @SystemControllerLog(description = "更改商品分类状态")
    public int updateSysProductCategoryAble(SysProductCategory productCategory) {
        logger.info("Call SystemProductCategoryAction.updateSysProductCategoryAble({})");
        return productCategoryService.updateSysProductCategory(productCategory);
    }
}
